package com.chainyoung.common.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chainyoung.common.R;
import com.chainyoung.common.common_widget.CustomToolbar;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.mvp.BasePresenter;

import org.simple.eventbus.EventBus;

import java.util.Objects;

import javax.inject.Inject;

public abstract class BaseFragment<T extends BasePresenter> extends SimpleFragment {

    protected RelativeLayout toolbar;
    //protected CustomToolbar toolbar;

    public ImageView mBarLeftImageView;
    public TextView mCurrentTtile;
    public ImageView mBarRightImageView;
    public ImageView mBarRightLeftImageView;
    public TextView mBarRightTextView;
    public TextView mBarLeftTextView;

    @Inject
    protected T mPresenter;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setupFragmentComponent(getAppComponent());
        EventBus.getDefault().register(this);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    protected void initUi() {
        toolbar = mView.findViewById(R.id.toolbar);
        if (toolbar != null) {
            initToolbar();
        }
    }


    private void initToolbar() {
        // setSupportActionBar(toolbar);
        mBarLeftImageView = ((ImageView) toolbar.findViewById(R.id.iv_left_image));
        mCurrentTtile = ((TextView) toolbar.findViewById(R.id.title));
        mBarRightImageView = ((ImageView) toolbar.findViewById(R.id.iv_toolbar_right));
        mBarRightTextView = ((TextView) toolbar.findViewById(R.id.tv_toolbar_right));
        mBarRightLeftImageView = ((ImageView) toolbar.findViewById(R.id.iv_toolbar_right_left));
        mBarLeftTextView = toolbar.findViewById(R.id.tv_toolbar_left);

        if (mBarLeftTextView != null) {
            mBarLeftTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onLeftTextViewClicked(v);
                }
            });
        }

        if (mBarLeftImageView != null) {
            mBarLeftImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onLeftViewClicked(v);
                }
            });
        }

        if (mBarRightImageView != null) {
            mBarRightImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRightImgClicked(v);
                }
            });
        }

        if (mBarRightLeftImageView != null) {
            mBarRightLeftImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRightLeftImgClicked(v);
                }
            });
        }

        if (mBarRightTextView != null) {
            mBarRightTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRightTxtClicked(v);
                }
            });
        }

    }

    public void onRightTxtClicked(View v) {

    }

    public void onRightLeftImgClicked(View v) {

    }

    public void onRightImgClicked(View v) {
    }


    /**
     * 标题栏左边被点击的方法
     * 默认为关闭当前界面
     */
    public void onLeftTextViewClicked(View view) {

    }


    /**
     * 标题栏左边被点击的方法
     * 默认为关闭当前界面
     */
    public void onLeftViewClicked(View view) {

    }

    /**
     * 设置bar最左边text
     *
     * @param rsID
     */
    public void showAndSetBarLeftText(int rsID) {
        showAndSetBarRightText(getString(rsID));
    }

    /**
     * 设置bar最左边text
     *
     * @param
     */
    public void showAndSetBarLeftText(String leftStr) {
        if (mBarLeftTextView != null) {
            mBarLeftTextView.setText(leftStr);
            mBarLeftTextView.setVisibility(View.VISIBLE);
            if (mBarLeftImageView != null) {
                mBarLeftImageView.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 设置bar最左边img
     *
     * @param
     */
    public void showAndSetBarLeftIcon(int rsID) {
        if (mBarLeftImageView != null) {
            mBarLeftImageView.setImageResource(rsID);
            mBarLeftImageView.setVisibility(View.VISIBLE);
        }
    }


    /**
     * 设置bar最右边图标
     *
     * @param rsID
     */
    public void showAndSetBarRightIcon(int rsID) {
        if (mBarRightImageView != null) {
            mBarRightImageView.setImageResource(rsID);
            mBarRightImageView.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 设置bar右边第二个图标
     *
     * @param rsID
     */
    public void showAndSetBarRightLeftIcon(int rsID) {
        if (mBarRightLeftImageView != null) {
            mBarRightLeftImageView.setImageResource(rsID);
            mBarRightLeftImageView.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 设置bar最右边text
     *
     * @param rsID
     */
    public void showAndSetBarRightText(int rsID) {
        showAndSetBarRightText(getString(rsID));
    }

    /**
     * 设置bar最右边text
     *
     * @param
     */
    public void showAndSetBarRightText(String rightStr) {
        if (mBarRightTextView != null) {
            mBarRightTextView.setText(rightStr);
            mBarRightTextView.setVisibility(View.VISIBLE);
        }
    }

    public void setCurrentTitle(int rsId) {
        if (mCurrentTtile != null) {
            mCurrentTtile.setText(getString(rsId));
        }

    }

    public void setCurrentTitle(String title) {
        if (mCurrentTtile != null) {
            mCurrentTtile.setText(title);
        }
    }

    public void setTitleDrawableRight(int rsId) {
        if (mCurrentTtile != null) {
            mCurrentTtile.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, rsId, 0);
        }
    }


    @Override
    public void showReloginDialog(int loginMsg) {
        ((BaseActivity) Objects.requireNonNull(getActivity())).showReloginDialog(loginMsg);
    }

    @Override
    public void onDestroyView() {
        if (mPresenter != null) {
            mPresenter.onDestroy();
        }
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }


    /**
     * 提供AppComponent(提供所有的单例对象)给子类，进行Component依赖
     */
    protected abstract void setupFragmentComponent(AppComponent appComponent);
}
